import socket

def client_demo(ip, port):
    # 创建一个套接字
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    try:
        # 连接到服务器
        client_socket.connect((ip, port))
        print(f"Connected to server at {ip}:{port}")

        # 发送消息
        message = "Hello from client!"
        client_socket.send(message.encode('utf-8'))
        print(f"Sent message: {message}")

    except Exception as e:
        print(f"Error: {e}")

    finally:
        # 关闭连接
        client_socket.close()
        print("Connection closed.")

if __name__ == "__main__":
    server_ip = "172.17.0.2"  # 修改为你的服务器 IP 地址
    server_port = 8820  # 修改为你的服务器端口
    client_demo(server_ip, server_port)